package com.wushijia.java8.time;

import java.time.Clock;
import java.time.DayOfWeek;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Month;
import java.time.MonthDay;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.temporal.ChronoField;
import java.util.Date;

/**
 * @author Benjamin Winterberg
 */
public class LocalDateTimeDemo01 {

  public static void main(String[] args) {

    LocalDateTime sylvester = LocalDateTime.of(2018, Month.MAY, 29, 23, 50, 59);
    System.out.println(sylvester.withYear(2));

    DayOfWeek dayOfWeek = sylvester.getDayOfWeek();
    System.out.println(dayOfWeek);      // WEDNESDAY

    Month month = sylvester.getMonth();
    System.out.println(month);          // DECEMBER

    long minuteOfDay = sylvester.getLong(ChronoField.MINUTE_OF_DAY);
    System.out.println(minuteOfDay);    // 1439

    //新旧日期转换
    Instant instant = sylvester.atZone(ZoneId.systemDefault()).toInstant();
    Date legacyDate = Date.from(instant);
    System.out.println(legacyDate);     // Wed Dec 31 23:59:59 CET 2014

    Date date = Date.from(LocalDate.now().atStartOfDay(ZoneId.systemDefault()).toInstant());
    System.out.println(date);

    System.out.println(LocalDateTime.ofInstant(new Date().toInstant(), ZoneId.systemDefault()));

    Duration duration = Duration.ofDays(5);
    System.out.println(duration);
    System.out.println(duration.plusHours(2).toMinutes());

    LocalDateTime localDateTime = LocalDateTime.now(Clock.systemDefaultZone());
    System.out.println(localDateTime.format(DateTimeFormatter.BASIC_ISO_DATE));
    System.out.println(localDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS")));

    //比较日期
    System.out.println(localDateTime.isBefore(sylvester));

    DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder();
    System.out.println(localDateTime.format(builder.appendPattern("yyyy-MM-dd").parseStrict().toFormatter()));

    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM dd, yyyy - HH:mm");
    LocalDateTime parsed = LocalDateTime.parse("04 03, 2014 - 07:13", formatter);
    System.out.println(parsed.format(formatter));

    //判断生日等
    MonthDay monthDay = MonthDay.of(parsed.getMonth(), parsed.getDayOfMonth());

  }
}